package com.coffeeloversupportlibrary.coffeelover.domain.article.adapter;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.collection.SparseArrayCompat;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.RecyclerView;

import com.coffeeloversupportlibrary.coffeelover.domain.article.type.ArticleCategoriesType;
import com.coffeeloversupportlibrary.coffeelover.domain.article.type.BaseType;
import com.coffeeloversupportlibrary.coffeelover.domain.article.type.GameType;
import com.coffeeloversupportlibrary.coffeelover.domain.article.type.PopularType;
import com.coffeeloversupportlibrary.coffeelover.domain.article.type.QuintessenceType;
import com.coffeeloversupportlibrary.coffeelover.domain.article.type.RecommendationType;
import com.coffeeloversupportlibrary.coffeelover.domain.article.type.SlideshowType;
import com.coffeeloversupportlibrary.coffeelover.uicompont.conflict.NestedScrollingParent2LayoutImpl3;

import java.util.List;

/**
 * @author puyv
 * @createTime 2021/11/6 20:11
 * @projectName CoffeeLoverSupportLibrary
 * @className MainAdapter.java
 * @description TODO
 * 11.10 改动信息：添加 FragmentManager 管理该Fragment 内部的子Fragment
 */
public class MainAdapter extends RecyclerView.Adapter {
    private static final int SLIDE_SHOW=1;
    private static final int RECOMMENDATION=2;
    private static final int QUINTESSENCE=3;
    private static final int POPULAR=4;
    private static final int GAME=5;
    private static final int ARTICLE_CATEGORIES=6;
    private FragmentManager fragmentManager;
    private NestedScrollingParent2LayoutImpl3 nestedScrollingParent2LayoutImpl3;

    SparseArrayCompat<Adapter> mCompat;
    private List<BaseType> mDataSource;


    public MainAdapter(List<BaseType> lists) {
        this.mDataSource=lists;
        this.mCompat=new SparseArrayCompat<Adapter>();
        this.mCompat.put(SLIDE_SHOW,new SlideshowAdapter());
        this.mCompat.put(RECOMMENDATION,new RecommendationAdapter());
        this.mCompat.put(QUINTESSENCE,new QuintessenceAdapter());
        this.mCompat.put(POPULAR,new PopularAdapter());
       // this.mCompat.put(GAME,new GameAdapter());
    }
    public MainAdapter(List<BaseType> lists,FragmentManager fragmentManager){
        this.mDataSource=lists;
        this.mCompat=new SparseArrayCompat<Adapter>();
        this.mCompat.put(SLIDE_SHOW,new SlideshowAdapter());
        this.mCompat.put(RECOMMENDATION,new RecommendationAdapter());
        this.mCompat.put(QUINTESSENCE,new QuintessenceAdapter());
        this.mCompat.put(POPULAR,new PopularAdapter());
        //this.mCompat.put(GAME,new GameAdapter());
        this.fragmentManager=fragmentManager;
        this.mCompat.put(ARTICLE_CATEGORIES,new ArticleCategoriesAdapter(this.fragmentManager));
    }
    public MainAdapter(List<BaseType> lists,FragmentManager fragmentManager,NestedScrollingParent2LayoutImpl3 nestedScrollingParent2LayoutImpl3){
        this.mDataSource=lists;
        this.mCompat=new SparseArrayCompat<Adapter>();
        this.mCompat.put(SLIDE_SHOW,new SlideshowAdapter());
        this.mCompat.put(RECOMMENDATION,new RecommendationAdapter());
        this.mCompat.put(QUINTESSENCE,new QuintessenceAdapter());
        this.mCompat.put(POPULAR,new PopularAdapter());
       // this.mCompat.put(GAME,new GameAdapter());
        this.fragmentManager=fragmentManager;
        this.nestedScrollingParent2LayoutImpl3=nestedScrollingParent2LayoutImpl3;
        this.mCompat.put(ARTICLE_CATEGORIES,new ArticleCategoriesAdapter(this.fragmentManager,this.nestedScrollingParent2LayoutImpl3));
    }

    public List<BaseType> getMDataSource() {
        return mDataSource;
    }

    public void setDataSource(List<BaseType> DataSource) {
        this.mDataSource = DataSource;
    }

    public void repeatBuild(FragmentManager fragmentManager,NestedScrollingParent2LayoutImpl3 nestedScrollingParent2LayoutImpl3) {
        this.fragmentManager = fragmentManager;
        this.nestedScrollingParent2LayoutImpl3=nestedScrollingParent2LayoutImpl3;
        this.mCompat.put(ARTICLE_CATEGORIES,new ArticleCategoriesAdapter(this.fragmentManager,this.nestedScrollingParent2LayoutImpl3));
    }



    @Override
    public int getItemViewType(int position) {
        if(mDataSource.get(position) instanceof SlideshowType){
            return SLIDE_SHOW;
        }else if(mDataSource.get(position) instanceof RecommendationType){
            return RECOMMENDATION;
        }else if(mDataSource.get(position) instanceof QuintessenceType){
            return QUINTESSENCE;
        }else if(mDataSource.get(position) instanceof PopularType){
            return POPULAR;
        }else if(mDataSource.get(position) instanceof GameType){
            return GAME;
        }else if(mDataSource.get(position) instanceof ArticleCategoriesType){
            return ARTICLE_CATEGORIES;
        }
        return super.getItemViewType(position);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return mCompat.get(viewType).onCreateViewHolder(parent);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        Adapter adapter = mCompat.get(holder.getItemViewType());
        adapter.onBindViewHolder(mDataSource, holder, position);

    }

    @Override
    public int getItemCount() {
        return mDataSource.size();
    }
}
